package common;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * BASE_Adapter.java
 *
 * @param <E>
 * @author 杨航
 * @version 1.0
 * @createTime 2016-7-15 上午11:29:31
 */

public abstract class BASE_Adapter<E> extends BaseAdapter {
    private Context mContext;
    private LayoutInflater inflater;
    private List<E> mData = new ArrayList<>();

    public BASE_Adapter(Context context) {
        this(context, null);
    }

    public BASE_Adapter(Context context, List<E> data) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(mContext);
        if (data != null){
            this.mData = data;
        }
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public E getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 添加一个数据
     *
     * @param item
     */
    public void addItem(E item) {
        if (item == null) {
            return;
        }
        mData.add(item);
        notifyDataSetChanged();
    }

    /**
     * 添加一组数据
     *
     * @param items
     */
    public void addItems(List<E> items) {
        if (items == null) {
            return;
        }
        mData.addAll(items);
        notifyDataSetChanged();
    }

    /**
     * 清空数据
     */
    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return createView(inflater, position, convertView, parent);
    }

    public abstract View createView(LayoutInflater inflater,
                                    int position, View convertView, ViewGroup parent);
}
